package ch.ethz.group3.myalbum.client;

//Yj9QS9PA3Ay5

import ch.ethz.group3.myalbum.client.utils.Global;
import ch.ethz.group3.myalbum.client.utils.Notifier;
import ch.ethz.group3.myalbum.shared.AuthorizationModel;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.Maps;
import com.google.gwt.maps.client.control.LargeMapControl;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

/*
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class MyAlbum implements EntryPoint {

	private MapWidget map;
	private PictureServiceAsync pictureService;

	// GWT module entry point method.
	public void onModuleLoad() {
		/*
		 * Asynchronously loads the Maps API.
		 * 
		 * The first parameter should be a valid Maps API Key to deploy this
		 * application on a public server, but a blank key will work for an
		 * application served from localhost.
		 */
		this.pictureService = GWT.create(PictureService.class);
		VLayout topBar = new VLayout();
		topBar.setWidth100();
		topBar.setHeight100();

		TopBarView topBarView = new TopBarView();
		topBar.addMember(topBarView);

		HLayout mainLayout = new HLayout();

		final GroupListPanel groupList = new GroupListPanel();
		groupList.setWidth("25%");
		groupList.setShowResizeBar(true);
		mainLayout.addMember(groupList);

		topBarView.addListener(groupList);

		final VLayout vLayout = new VLayout();
		vLayout.setWidth("75%");

		final ImageGridPanel imagePanel = new ImageGridPanel();
		imagePanel.setHeight("85%");
		vLayout.addMember(imagePanel);
		

		groupList.setRecordClickHandler(new RecordClickHandler() {
			@Override
			public void onRecordClick(RecordClickEvent event) {
				Record rec = event.getRecord();
				if (rec instanceof GroupListRecord) {
					GroupListRecord group = (GroupListRecord) rec;
					imagePanel.loadGroupImages(group.getAttributeAsString("groupId"));
				}
			}
		});

		// AIzaSyDmjCi108D1kxxKH56CyTojNOpGtIILFNI
		Maps.loadMapsApi("", "2", false, new Runnable() {
			public void run() {
				map = new MapWidget(LatLng.newInstance(0, 0),3);
				map.setSize("100%", "100%");
				map.addControl(new LargeMapControl());	
				map.setStyleName("bordertop");
				map.setHeight("300px");
				map.setPinchToZoom(true);
				
				vLayout.addMember(map);
				imagePanel.setMap(map);
			}
		});

		mainLayout.addMember(vLayout);
		topBar.addMember(mainLayout);

		topBar.draw();
		
		if (Global.accessToken == null) {
			pictureService.getAuthorization(new AsyncCallback<AuthorizationModel>() {
				
				@Override
				public void onSuccess(AuthorizationModel result) {
					Notifier.notify(result);
				}
				
				@Override
				public void onFailure(Throwable caught) {
				}
			});
		}
		
	}
}